package com.sql.test.service;

import android.widget.Toast;

import java.util.ArrayList;

import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.geom.Vec3;

/**
 * Created by Lenovo on 2018/4/22.
 */

public class DataFormatService {

    private  String geomType = null;
    private  ArrayList<Position> positions =null;
    private  ArrayList<Position[]> multiPositions = null;

    public String getGeomType(String geomText) {

            //geomText : POINT(),POINT Z(),LineString( , , ),Polygon( , , ,),MultiPoint(( ),( )),MultiLineString(( , ,),( , ,)),MultiPolygon(( , ,),( , ,))
            geomType = geomText.split(" ")[0].trim();
            if (geomType.contains("(")) {
                geomType = geomType.split("\\(")[0].trim();
            } else {
                geomType += "Z";
            }
            //确保大写格式，便于判断
            return geomType.toUpperCase();
    }
    public ArrayList<Position> getPositions(String geomText) {
        positions = new ArrayList<>();
        getGeomType(geomText);
        switch (geomType){
            case "POINT":
             //point(x y) -->Position(longitude,latitude,altitude),x-->latitude,y-->longitude
             Double[] point = pointFromText(geomText);
             Position positon = new Position(point[1],point[0],0);
             positions.add(positon);
                break;
            case "LINESTRING":
                simpleGeometryFromText(geomText);
                break;
            case "MULTIPOINT":
                simpleGeometryFromText(geomText);
                break;
        }
        return positions;
    }

    public ArrayList<Position[]> getMultiPositions(String geomText) {
        multiPositions = new ArrayList<>();
        getGeomType(geomText);
        switch (geomType){
            case "POLYGON":
                multiGeometryFromText(geomText);
                break;
            case "MULTILINESTRING":
                multiGeometryFromText(geomText);
                break;
            case "MULTIPOLYGON":
                multiGeometryFromText(geomText);
                break;
        }
        return multiPositions;
    }

    public  Double[] pointZFromText(String pointZText){
        //pointText example  : POINT Z(x y z)
        Double [] point = new Double[3];
        geomType = pointZText.split(" ")[0];
        point[0] = Double.parseDouble(pointZText.split(" ")[1].split("\\(")[1]);
        point[1] = Double.parseDouble(pointZText.split(" ")[2]);
        point[2] = Double.parseDouble(pointZText.split(" ")[3].split("\\)")[0]);
        return point;
    }
    public  Double[] pointFromText(String pointText){
        //pointText example : POINT(x y)
        Double [] point = new Double[2];
        point[0] = Double.parseDouble(pointText.split(" ")[0].split("\\(")[1]);
        point[1] = Double.parseDouble(pointText.split(" ")[1].split("\\)")[0]);
        return point;
    }

   /** geometryText LineString(,,) Polygon( ,,),MultiPoint(,,)
    **/
    public void simpleGeometryFromText(String simpleGeometryText){
        //geometryText LineString(1 2, 3 4, 5 7) ,MultiPoint(,,)
        Position position = null;
        String[] first =simpleGeometryText.split("\\(");
        String[] second = first[1].trim().split("\\)");
        String[] third = second[0].trim().split(",");
        for(int i=0;i<third.length;i++){
            //匹配一个或多个空白符\\s+
            String[] forth = third[i].trim().split("\\s+");
            //POINT(x y)-->Position(latitude,longitude,altitude):x-->longitude,y-->latitude，注意xy与经纬度的对应关系
            position= new Position(Double.parseDouble(forth[1]),Double.parseDouble(forth[0]),0);
            positions.add(position);
        }
    }
    /*** to solve multi geometry
     * //Polygon(( ,, )),MultiLineString((,,),(,,)) MulitPolygon((,,),(,,))
     ***/
    public Double[][][] multiGeometryFromText(String multiGeometryText){
    //Polygon(( ,, )),MultiLineString((,,),(,,)) MulitPolygon((,,),(,,))
        Position position = null;
        Position[] tempositions = null;
        String[] zero = multiGeometryText.split("\\(\\(");
        zero = zero[1].trim().split("\\)\\)");
        //*匹配0个或多个前一个字符，此处为空格符
        String[] first = zero[0].trim().split("\\), *\\(");
        int geometryNumber = first.length;
        String[][] second = new String[geometryNumber][];
        Double[][][] third = new Double[geometryNumber][][];
        for(int i=0;i<geometryNumber;i++){
            second[i]=first[i].trim().split(",");
            int pointNumber = second[i].length;
            tempositions = new Position[pointNumber];
            third[i]=new Double[pointNumber][];
            for(int j=0;j<pointNumber;j++){
                third[i][j] = new Double[2];
                for(int k=0;k<2;k++){
                    //匹配一个或多个空白符\\s+
                    third[i][j][k] = Double.parseDouble(second[i][j].trim().split("\\s+")[k]);
                }
                //POINT(x y)-->Position(latitude,longitude,altitude):x-->longitude,y-->latitude，注意xy与经纬度的对应关系
                position = new Position(third[i][j][1],third[i][j][0],0);
                tempositions[j]=position;
            }
            multiPositions.add(tempositions);
        }
        return third;
    }

    public String geometryToText(ArrayList<Position> positions,String type){
        String geometryString = null;
        switch (type){
            case "POINT":
                //POINT(x y)-->Position(latitude,longitude,altitude):x-->longitude,y-->latitude，注意xy与经纬度的对应关系
                geometryString = "'POINT("+String.valueOf(positions.get(0).longitude)+" "+String.valueOf(positions.get(0).latitude)+")'";
                break;
            case "POLYLINE":
                geometryString = "'LINESTRING(";
                for(int i=0;i<positions.size()-1;i++){
                    geometryString+=String.valueOf(positions.get(i).longitude)+" "+String.valueOf(positions.get(i).latitude)+",";
                }
                geometryString+=String.valueOf(positions.get(positions.size()-1).longitude)+" "+String.valueOf(positions.get(positions.size()-1).latitude)+")'";
                break;
            case "POLYGON":
                //与point，linstring在格式上有区别，是双括号
                geometryString = "'POLYGON((";
                for(int i=0;i<positions.size()-1;i++){
                    geometryString+=String.valueOf(positions.get(i).longitude)+" "+String.valueOf(positions.get(i).latitude)+",";
                }
                geometryString+=String.valueOf(positions.get(positions.size()-1).longitude)+" "+String.valueOf(positions.get(positions.size()-1).latitude)+"))'";
                break;
        }
        return geometryString;
    }


}

